<?php
namespace App\models\user;

use App\models\Notification;
use App\other\AdminFlag;
use DB;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Str;
use Redis;
use Session;

/**
 * App\models\user\User
 *
 * @property int $id
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string $truename
 * @property string $profile
 * @property string $city
 * @property string $company
 * @property string $weiboname
 * @property string $weibourl
 * @property string $weblink
 * @property string $wechatqrcode
 * @property string $paymentqrcode
 * @property string $introduction
 * @property bool $verified
 * @property bool $notify
 * @property bool $read_flag
 * @property bool $is_ban
 * @property int $ban_time
 * @property string $remember_token
 * @property string $last_actived_at
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property string $deleted_at
 * @property-read \App\models\user\UserRoles $userRole
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $readNotifications
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $unreadNotifications
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereEmail($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User wherePassword($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereTruename($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereProfile($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereCity($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereCompany($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereWeiboname($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereWeibourl($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereWeblink($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereWechatqrcode($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User wherePaymentqrcode($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereIntroduction($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereVerified($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereNotify($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereReadFlag($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereIsBan($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereBanTime($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereRememberToken($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereLastActivedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereDeletedAt($value)
 * @mixin \Eloquent
 * @property string $session_id
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereSessionId($value)
 * @property-read \App\models\user\SteamUser $steam
 * @property int $notification_count
 * @method static \Illuminate\Database\Query\Builder|\App\models\user\User whereNotificationCount($value)
 */
class User extends Authenticatable{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getSteamUser(){
        return $this->belongsTo(SteamUser::class, 'id', 'uid');
    }

    public function steam(){
        return $this->belongsTo(SteamUser::class, 'id', 'uid');
    }

    public function userRole(){
        return $this->belongsTo(UserRoles::class, 'id', 'uid');
    }

    public function notifications(){
        return $this->belongsTo(Notification::class, 'id', 'uid');
    }

    public function recordLastActivedAt($is_save = false){
        $this->last_actived_at = date('Y-m-d H:i:s');
        $this->session_id = Session::getId();
        $s_key = 'la_time'.$this->id;
        $time = time();
        if(Session::has($s_key)){
            $time = intval(Session::get($s_key));
        }else{
            Session::push($s_key, time() + 60);
        }
        if ($is_save == true || $time  < time()){
            $this->save();
            Session::remove($s_key);
            Session::push($s_key, time() + 60);
        }
    }

    public function lastActivedAt(){
        return $this->last_actived_at;
    }

    public function registerIndex(){
        return self::where('id', '<=', $this->id)->count();
    }

    public function deleteUserSession(){
        Redis::command('del', ['laravel:'.$this->session_id]);
        $this->session_id           = Str::random(32);
        $this->remember_token       = Str::random(60);
    }

    public function roleLevel(){
        return $this->userRole->role->level;
    }

    public function  roleFlag(){
        return $this->userRole->role->flag;
    }

    public static function getUserByFlag(int $flag){
        return DB::select('SELECT u.* FROM users as u, user_roles as us, roles as r WHERE u.id = us.uid and us.role_id = r.id and r.flag & ?;', [$flag]);
    }

    public static function getPlateAdmins(int $plate_id){
        return DB::select(
            'SELECT u.id, u.`name`, u.`profile`, r.display_name, m.created_at,m.id as mid FROM users AS u,moderators AS m, user_roles AS ur, roles AS r, `partitions` as p 
            WHERE u.id = ur.uid AND u.id = m.uid AND p.id = ? AND ur.role_id = r.id AND m.plate_id = p.id AND r.flag & ?', [$plate_id, AdminFlag::ADMIN_FLAG_TOPIC_ED]);
    }
}
